﻿using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.CVP
{
    public partial class AddCVP : Form
    {
        NursingFormRecord record = new NursingFormRecord();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;//选前选中患者信息，数组，传入调用的form中
        string Id;
        public AddCVP(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            this.Id = id;
            BindInfo(id);
        }
        public AddCVP(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = string.Empty;
            BindInfo(this.Id);
        }
        public AddCVP(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = id;
            BindInfo(this.Id);
        }
        #region 保存
        private void simplebaocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(SaveDate.Text))
                SaveDate.DateTime = DateTime.Now;
            CVPS cvp = new CVPS();
            cvp.FormId = (int)FormType.CVPS;
            cvp.FormName = EnumHelper.GetDescription(FormType.CVPS);
            cvp.JianCeRiQi = SaveDate.Text;
            cvp.ShiJian = DateTimeTran.TimeFillZero(SaveTime.Text);
            cvp.ZhongXinJingMaiYa = txtCVP.Text;
            record.NursingFormData = XmlUtil.ObjectToXML(cvp, Encoding.UTF8);
            record.FormId = cvp.FormId;//表单ID
            record.FormName = cvp.FormName;//表单NAME
            record.PatientId = PatientInfo.PATIENT_ID;//住院号
            record.VisitId = PatientInfo.VISIT_ID;//住院次数
            record.PatientName = PatientInfo.PATIENT_NAME;//患者名
            record.BedNo = PatientInfo.BED_NO;//床号
            record.DepartmentId = PatientInfo.DEPARTMENT_ID;//科室ID
            record.DepartmentName = PatientInfo.DEPARTMENT_NAME;//科室名称
            record.AreaId = PatientInfo.NURSE_CELL_CODE;//病区编号
            record.AreaName = PatientInfo.NURSE_CELL_NAME;//病区名称
            record.AssessmentDate = Convert.ToDateTime(SaveDate.Text + " " + cvp.ShiJian);//日期       
            record.Id = string.IsNullOrEmpty(Id) ? 0 : Convert.ToInt32(Id);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }
        }
        #endregion

        #region 取消
        private void simplequxiao_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        private void BindInfo(string id)
        {
            this.SaveDate.DateTime = DateTime.Now; //显示当前日期
            this.SaveTime.DateTime = DateTime.Now; //显示当前时间
            if (string.IsNullOrEmpty(id)) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            CVPS ps = new CVPS();
            ps = XmlUtil.XMLToObject<CVPS>(NursingFormData, Encoding.UTF8);
            SaveDate.Text = ps.JianCeRiQi;
            SaveTime.Text = DateTimeHelper.HHmmssToHHmm(ps.ShiJian);
            txtCVP.Text = ps.ZhongXinJingMaiYa;
        }
    }
}